import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class TestVolcanoConfigService:
    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/1_get_numa_specific_cr.yaml'))
    def test_get_specific_cr(self, volcanoinfo, get_env_info):
        # 获取NUMA资源相关实例接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{group}',datas.numa_instance_group).replace('{version}',datas.numa_instance_version).replace('{plural}',datas.numa_instance_plural).replace('{name}',datas.numa_instance_name)
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/2_get_numa_cr_list.yaml'))
    def test_get_numa_cr_list(self, volcanoinfo, get_env_info):
        # 获取NUMA资源相关实例列表接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{group}',datas.numa_instance_group).replace('{version}',datas.numa_instance_version).replace('{plural}',datas.numa_instance_plural)
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/3_expression_query.yaml'))
    def test_expression_query(self, volcanoinfo, get_env_info):
        # 表达式查询接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{expression}','numa_nodes_count')
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/4_scheduler_policy.yaml'))
    def test_get_scheduler_policy(self, volcanoinfo, get_env_info):
        # 获取和调度策略接口-numa亲和性、binpack、drf、gang、priority、恢复默认设置
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        if 'body' in volcanoinfo['request']:
            body = volcanoinfo['request']['body']
            req = RequestUtil().send_request(method, url, json=body)
        else:
            req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/5_get_numa_node_list.yaml'))
    def test_get_numa_node_list(self, volcanoinfo, get_env_info):
        # 获取NUMA Node列表接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/6_get_numatopologies.yaml'))
    def test_get_numatopologies(self, volcanoinfo, get_env_info):
        # 获取numa topology接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{name}',datas.numa_instance_name)
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/7_get_podname_by_uuid.yaml'))
    def test_get_podname_by_uuid(self, volcanoinfo, get_env_info):
        # 获取NUMA Node列表接口
        baseUrl = get_env_info
        url1 = baseUrl + '/clusters/host/rest/scheduling/api/v1/query?query=container_numa_node_assignment%7Bnode%3D%27fuyao-master%27%7D'
        method = volcanoinfo['request']['method']
        req1 = RequestUtil().send_request(method, url1)
        if len(req1.json()['data']['result']) != 0 :
            globals()['uuid'] = req1.json()['data']['result'][0]['metric']['pod_uuid']
            url2 = baseUrl + volcanoinfo['request']['url'].replace('{uuid}',globals()['uuid'])
            req2 = RequestUtil().send_request(method, url2)
            assert req2.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/8_get_priorityclass_list.yaml'))
    def test_get_priorityclass_list(self, volcanoinfo, get_env_info):
        # 获取优先级队列列表接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/9_create_priorityclass.yaml'))
    def test_create_priorityclass(self, volcanoinfo, get_env_info):
        # 创建优先级队列列表接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        req = RequestUtil().send_request(method, url,json=body)
        if len(req.json()['metadata']) != 0:
            globals()['priorityclassname']  = req.json()['metadata']['name']
        else:
            print('Create PriorityClasses Failed')
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/10_get_specific_priorityclass.yaml'))
    def test_get_specific_priorityclass(self, volcanoinfo, get_env_info):
        # 获取指定优先级队列接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{priorityclassname}',globals()['priorityclassname'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/11_modify_specific_priorityclass.yaml'))
    def test_modify_specific_priorityclass(self, volcanoinfo, get_env_info):
        # 更新指定优先级队列接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{priorityclassname}',globals()['priorityclassname'])
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        req = RequestUtil().send_request(method, url,json=body)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/12_update_section_specific_priorityclass.yaml'))
    def test_update_specific_priorityclass(self, volcanoinfo, get_env_info):
        # 部分更新指定优先级队列接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{priorityclassname}',globals()['priorityclassname'])
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        header = volcanoinfo['request']['header']
        req = RequestUtil().send_request(method, url,json=body,headers=header)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/13_delete_priorityclass.yaml'))
    def test_delete_priorityclass(self, volcanoinfo, get_env_info):
        # 删除优先级队列接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{priorityclassname}',globals()['priorityclassname'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/14_get_podgroup_list.yaml'))
    def test_get_podgroup_list(self, volcanoinfo, get_env_info):
        # 获取podgroup列表接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/15_create_podgroup.yaml'))
    def test_create_podgroup(self, volcanoinfo, get_env_info):
        # 创建podgroup接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default')
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        req = RequestUtil().send_request(method, url,json=body)
        if '-' not in volcanoinfo['name']:
            globals()['podgroupname']  = req.json()['metadata']['name']
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/16_get_specific_podgroup.yaml'))
    def test_get_specific_podgroup(self, volcanoinfo, get_env_info):
        # 获取指定podgroup接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{podgroupname}',globals()['podgroupname'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in volcanoinfo['name']:
            updatereqbody = req.json()
            globals()['updatereqbody'] = updatereqbody
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/17_modify_specific_podgroup.yaml'))
    def test_modify_specific_podgroup(self, volcanoinfo, get_env_info):
        # 更新指定podgroup接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{podgroupname}',globals()['podgroupname'])
        method = volcanoinfo['request']['method']
        body = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url,json=body)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/18_update_section_specific_podgroup.yaml'))
    def test_update_specific_podgroup(self, volcanoinfo, get_env_info):
        # 部分更新指定podgroup接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{podgroupname}',globals()['podgroupname'])
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        header = volcanoinfo['request']['header']
        req = RequestUtil().send_request(method, url,json=body,headers=header)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/19_delete_podgroup.yaml'))
    def test_delete_podgroup(self, volcanoinfo, get_env_info):
        # 删除podgroup接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{podgroupname}',globals()['podgroupname'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in volcanoinfo['name']:
            assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/20_get_queue_list.yaml'))
    def test_get_queue_list(self, volcanoinfo, get_env_info):
        # 获取queue列表接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/21_create_queue.yaml'))
    def test_create_queue(self, volcanoinfo, get_env_info):
        # 创建queue接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        req = RequestUtil().send_request(method, url,json=body)
        if '-' not in volcanoinfo['name']:
            globals()['queuename']  = req.json()['metadata']['name']
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/22_get_specific_queue.yaml'))
    def test_get_specific_queue(self, volcanoinfo, get_env_info):
        # 获取指定queue接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{queuename}',globals()['queuename'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in volcanoinfo['name']:
            updatereqbody = req.json()
            globals()['updatereqbody'] = updatereqbody
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/23_modify_specific_queue.yaml'))
    def test_modify_specific_queue(self, volcanoinfo, get_env_info):
        # 更新指定queue接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{queuename}',globals()['queuename'])
        method = volcanoinfo['request']['method']
        body = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url,json=body)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/24_update_section_specific_queue.yaml'))
    def test_update_specific_queue(self, volcanoinfo, get_env_info):
        # 部分更新指定queue接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{queuename}',globals()['queuename'])
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        header = volcanoinfo['request']['header']
        req = RequestUtil().send_request(method, url,json=body,headers=header)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/25_delete_queue.yaml'))
    def test_delete_queue(self, volcanoinfo, get_env_info):
        # 删除queue接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{queuename}',globals()['queuename'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in volcanoinfo['name']:
            assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/26_get_vcjob_list.yaml'))
    def test_get_vcjob_list(self, volcanoinfo, get_env_info):
        # 获取vcjob列表接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url']
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/27_create_vcjob.yaml'))
    def test_create_vcjob(self, volcanoinfo, get_env_info):
        # 创建vcjob接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default')
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        req = RequestUtil().send_request(method, url,json=body)
        if '-' not in volcanoinfo['name']:
            globals()['vcjobname']  = req.json()['metadata']['name']
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/28_get_specific_vcjob.yaml'))
    def test_get_specific_vcjob(self, volcanoinfo, get_env_info):
        # 获取指定vcjob接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{vcjobname}',globals()['vcjobname'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in volcanoinfo['name']:
            updatereqbody = req.json()
            globals()['updatereqbody'] = updatereqbody
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/29_modify_specific_vcjob.yaml'))
    def test_modify_specific_vcjob(self, volcanoinfo, get_env_info):
        # 更新指定vcjob接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{vcjobname}',globals()['vcjobname'])
        method = volcanoinfo['request']['method']
        body = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url,json=body)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/30_update_section_specific_vcjob.yaml'))
    def test_update_specific_vcjob(self, volcanoinfo, get_env_info):
        # 部分更新指定vcjob接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{vcjobname}',globals()['vcjobname'])
        method = volcanoinfo['request']['method']
        body = volcanoinfo['request']['body']
        header = volcanoinfo['request']['header']
        req = RequestUtil().send_request(method, url,json=body,headers=header)
        assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('volcanoinfo', YamlUtil().read_testcase_yaml('v24.09/volcanoConfigService/31_delete_vcjob.yaml'))
    def test_delete_vcjob(self, volcanoinfo, get_env_info):
        # 删除vcjob接口
        baseUrl = get_env_info
        url = baseUrl + volcanoinfo['request']['url'].replace('{namespace}','default').replace('{vcjobname}',globals()['vcjobname'])
        method = volcanoinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '-' not in volcanoinfo['name']:
            assert req.status_code == volcanoinfo['validate'][0]['equals']['status_code']